package main.prototype;

import java.io.*;

/**
 * @author: kejl
 * @create: 2020-07-15 00:21
 */
public class ConcretePrototype_B implements AbstractPrototype {
    @Override
    public void showdisplayname() {
        System.out.println(this.getClass().hashCode());

    }

    @Override
    public AbstractPrototype clone()  {
        AbstractPrototype abstractPrototype=null;
        try {
            abstractPrototype =  (AbstractPrototype) super.clone();
        }catch (Exception e){
            e.printStackTrace();
        }
        return abstractPrototype;
    }

    //使用序列化技术实现深克隆

    public  AbstractPrototype deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bao);
        oos.writeObject(this);
        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        return (AbstractPrototype)ois.readObject();

    }
}
